<template>
  <div
    class="jp-wrapper"
    :class="{ 'jp-sidebar--fold': sidebarFold }">
    <template>
      <main-navbar ref="navbar" @showRight="showRight" />
      <main-sidebar/>
      <div class="jp-content__wrapper" :style="{ 'min-height': documentClientHeight + 'px' }">
        <main-content/>
      </div>
      <main-right ref="mainRight"/>
    </template>
  </div>
</template>

<script>
  import MainNavbar from './layout/_common_top'
  import MainSidebar from './layout/_common_left'
  import MainContent from './layout/_common_center'
  import MainRight from './layout/_common_right'
  import {
    setLocalStorage,
    getLocalStorage
  } from '@/utils/localstore.js'
  export default {
    data () {
      return {
        isRightVisible: false
      }
    },
    components: {
      MainNavbar,
      MainSidebar,
      MainContent,
      MainRight
    },
    computed: {
      documentClientHeight: {
        get () {
          return this.$store.state.common.documentClientHeight
        },
        set (val) {
          this.$store.commit('common/updateDocumentClientHeight', val)
        }
      },
      sidebarFold: {
        get () {
          return this.$store.state.common.sidebarFold
        }
      }
    },
    created () {
      this.$nextTick(() => {
        if (typeof (window.websock) === 'undefined') {
                 this.initWebSocket()
          }
      })
    },
    mounted () {
      this.getUserInfo()
      this.getConfig()
      this.resetDocumentClientHeight()
    },
    methods: {
      // 初始化连接 webSocktet
        initWebSocket () { // 初始化weosocket
          console.log(1111)
        // if(this.$store.state.ws.flage !==1　){
         // const wsUri = "ws://192.168.0.109:8080/ws/asset/12345/3";
        if (typeof WebSocket === 'undefined') {
            alert('您的浏览器不支持webSocket')
        } else {
            let wsUri = ''
            let dbName = getLocalStorage('bookDbName')
            let token = this.$cookie.get('token')
            if (process.env.NODE_ENV === 'development') {
              // wsUri='ws://192.168.100.33:8080/ws/asset/'+token+'/'+dbName+'/3'
              // wsUri = 'ws://192.168.0.109:8080/ws/asset/' + token + '/' + dbName + '/3'
              wsUri = 'wss://ygb.chengfengkuaiyun.com/jeeplus/ws/asset/' + token + '/' + dbName + '/3'
              // wsUri = 'ws://39.106.77.144:8088/jeeplus/ws/asset/' + token + '/' + dbName + '/3'
            } else {
              wsUri = 'wss://ygb.chengfengkuaiyun.com/jeeplus/ws/asset/' + token + '/' + dbName + '/3'
            }
            window.websock = new WebSocket(wsUri)
            // }
            window.websock.onmessage = this.websocketonmessage
            window.websock.onopen = this.websocketonopen
            window.websock.onerror = this.websocketonerror
            window.websock.onclose = this.websocketclose
        }
      },
      // websocket连接成功
        websocketonopen () { // 连接建立之后执行send方法发送数据
            let actions = {'test': '链接成功了'}
          this.$store.commit('ws/setFlage', 1)
          // 添加状态判断，当为OPEN时，发送消息
          console.log('ws状态=======', window.websock)
          if (window.websock.readyState === 1) {
            this.websocketsend(JSON.stringify(actions))
          } else {
            // do something
            console.log('ws状态=======失败', window.websock)
          }
      },
        websocketonerror () { // 连接建立失败重连
            this.initWebSocket()
      },

      // 监控数据接收
        websocketonmessage (e) {
            this.$store.commit('ws/setResData', JSON.parse(e.data))
            // 全局预警提示
             // this.$message2.warning(JSON.parse(e.data)[0].warnInfo.length,{text:JSON.parse(e.data)[0].warnInfo,duration:3000000} , function callBack(){

             //      })
            // window.JSON.parse(window.resData.weightMonitor)
        },

      // 数据发送

        websocketsend (data) {
            window.websock.send(data)
      },
        websocketclose (e) {  // 关闭
            this.websocketonopen()
            // this.initWebSocket()
      },
      // 重置窗口可视高度
      resetDocumentClientHeight () {
        this.documentClientHeight = document.documentElement['clientHeight']
        window.onresize = () => {
          this.documentClientHeight = document.documentElement['clientHeight']
          if (this.$refs.navbar) {
            let _defaultLayout = this.$refs.navbar.defaultLayout
            if (_defaultLayout === 'top') {
              this.$refs.navbar.fixTopMenu()
            }
          }
        }
      },
      showRight (flag) {
        this.$refs.mainRight.showRight()
        this.isRightVisible = flag
      },
      // 获取当前登录用户信息
      getUserInfo () {
        this.$http({
          url: '/sys/user/info',
          method: 'get'
        }).then(({data}) => {
          if (data.success) {
            this.$store.commit('user/updateUser', data.user)
          }
        })
      },
      // 获取产品name 和 logo
      getConfig () {
        this.$http.get('/sys/sysConfig/queryById').then(({data}) => {
          if (data.success) {
            this.$store.commit('config/updateProductName', '租户管理系统')
            // this.$store.commit('config/updateLogo', '/static/logo/logo.png')
            if (!localStorage.getItem('defaultLayout')) {
              this.$store.commit('config/updateDefaultLayout', data.config.defaultLayout)
            }
            if (!localStorage.getItem('defaultTheme')) {
              this.$store.commit('config/updateDefaultTheme', data.config.defaultTheme)
            }
          }
        })
      }
    }
  }
</script>
